﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace A2.ERP.DALFactory
{
    /// <summary>
    /// 抽象工厂：都是解决对象的创建问题（抽象工厂是通过反射的方式创建类型的实例）
    /// </summary>
    public class DALAbastr
    {
        private static readonly string DalNameSpace = ConfigurationManager.AppSettings["DalNameSpace"];//获取命名空间

        private static readonly string DalAssembly = ConfigurationManager.AppSettings["DalAssembly"];

        //public static IUserInfoDAL CreateUserInfoDAL()
        //{
        //    string fullClassName = DalNameSpace + ".UserInfoDAL";//构建类的全名称

        //    return CreateInstance(fullClassName, DalAssembly) as IUserInfoDAL;
        //}

        public static object CreateDAL<T>()
        {
            string fullClassName = DalNameSpace + "." + typeof(T).Name.ToString();//构建类的全名称

            return CreateInstance(fullClassName, DalAssembly);
        }


        private static object CreateInstance(string fullClassName, string DalAssembly)
        {
            var assembly = Assembly.Load(DalAssembly);//加载程序集

            return assembly.CreateInstance(fullClassName);
        }
    }
}
